﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace Service
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class MyServer : IServer
    {

        public string IsValidLoginPassword(string login, string password)
        {
            var db = new DBConnectorDataContext();

            foreach (var worker in db.Workers)
            {
                Debug.WriteLine(string.Format("Login: '{0}'", worker.Name));
                Debug.WriteLine(string.Format("Password: '{0}'", worker.Password));

                if (worker.Name == login && worker.Password.Split(new []{" "}, StringSplitOptions.RemoveEmptyEntries)[0] == password)
                    return worker.Mode;
                
            }
            return null;
        }

        public string RegistryLogin(string login, string password, string post, int salary)
        {
            var db = new DBConnectorDataContext();

            var user = (from u in db.Workers
                        where u.Name == login
                        select u).ToList();
            
            if (user.Count == 1) return "Неверный логин";
            
            // роль 2 - обычный юзер
            db.Workers.InsertOnSubmit(new Worker { Name = login, Password = password, Post = post, Salary = salary, Mode = "user", id = db.Workers.Count() });
            db.SubmitChanges();

            return "Логин успешно зарегистрирован!";
        }

        public string SetPost(string login, string Post)
        {
            try
            {
                var db = new DBConnectorDataContext();

                var user = (from u in db.Workers
                            where u.Name == login
                            select u).First();

                user.Post = Post;
                db.SubmitChanges();

                return "success";
            }
            catch (Exception)
            {
            }

            return null;
        }

        public string SetMode(string login, string Mode)
        {
            try
            {
                var db = new DBConnectorDataContext();

                var user = (from u in db.Workers
                            where u.Name == login
                            select u).First();

                user.Mode = Mode;
                db.SubmitChanges();

                return "success";
            }
            catch (Exception)
            {
            }

            return null;
        }

        public string SetPassword(string login, string password)
        {
            try
            {
                var db = new DBConnectorDataContext();

                var user = (from u in db.Workers
                            where u.Name == login
                            select u).First();

                user.Password = password;
                db.SubmitChanges();

                return "success";
            }
            catch (Exception)
            {
            }

            return null;
        }

        public string SetSalary(string login, int Salary)
        {
            try
            {
                var db = new DBConnectorDataContext();

                var user = (from u in db.Workers
                            where u.Name == login
                            select u).First();

                user.Salary = Salary;
                db.SubmitChanges();

                return "success";
            }
            catch (Exception)
            {
            }

            return null;
        }

        public void DeleteWorker(string login)
        {
            try
            {
                var db = new DBConnectorDataContext();

                var user = (from u in db.Workers
                            where u.Name == login
                            select u).First();

                db.Workers.DeleteOnSubmit(user);
                db.SubmitChanges();

            }
            catch (Exception)
            {
            }
        }

        public IEnumerable<User> GetUsers()
        {
            var db = new DBConnectorDataContext();
            return from u in db.Workers
                   select new User {Name = u.Name, Post = u.Post, Salary = u.Salary};
        }
    }
}
